import {
  deleteMessage,
  getMessageSendList,
  type Message,
  recallMessage,
  sendMessage
} from "@/api/message";
import type { PaginationProps } from "@pureadmin/table";
import { onMounted, reactive, ref } from "vue";
import { message } from "@/utils/message";
import { DictTag } from "@/components/ReDict";
import { useRouter } from "vue-router";

export function userMessage() {
  const router = useRouter();
  const form = reactive<Message>({
    current: 1,
    size: 20,
    status: ""
  });
  const dataList = ref([]);
  const loading = ref(true);
  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 10,
    currentPage: 1
  });
  const columns: TableColumnList = [
    {
      label: "标题",
      prop: "title"
    },
    {
      label: "状态",
      prop: "status",
      cellRenderer: ({ row, props }) => (
        <DictTag
          size={props.size}
          value={row.status}
          code="Message_Send_Status"
        />
      )
    },
    {
      label: "操作",
      fixed: "right",
      slot: "operation"
    }
  ];

  function handleSizeChange(val: number) {
    form.size = val;
    onSearch();
  }

  function handleCurrentChange(val: number) {
    form.current = val;
    onSearch();
  }

  function onSearch() {
    loading.value = true;
    getMessageSendList(form)
      .then(data => {
        dataList.value = data.records;
        pagination.total = data.total;
        pagination.pageSize = data.size;
        pagination.currentPage = data.current;
      })
      .finally(() => {
        loading.value = false;
      });
  }

  const resetForm = (formEl: any) => {
    if (!formEl) return;
    formEl.resetFields();
    onSearch();
  };

  function handleDelete(row: Message) {
    deleteMessage(row.id)
      .then(() => {
        message("消息删除成功", { type: "success" });
        onSearch();
      })
      .catch(err => {
        message(`消息删除失败:${err}`, { type: "error" });
      });
  }

  function addMessage() {
    // 路由跳转
    router.push({ name: "SendMessage" });
  }

  function editMessage(id: string) {
    router.push({ name: "SendMessage", query: { id: id } });
  }

  function viewMessage(id: string) {
    router.push({ name: "ViewMessage", query: { id: id } });
  }

  function send(id: string) {
    sendMessage(id)
      .then(() => {
        message("消息发送成功", { type: "success" });
      })
      .catch(err => {
        message(`消息发送失败:${err}`, { type: "error" });
      })
      .finally(() => {
        onSearch();
      });
  }

  function recall(id: string) {
    recallMessage(id)
      .then(() => {
        message("消息撤回成功", { type: "success" });
      })
      .catch(err => {
        message(`消息撤回失败:${err}`, { type: "error" });
      })
      .finally(() => {
        onSearch();
      });
  }

  onMounted(() => {
    onSearch();
  });

  return {
    form,
    loading,
    columns,
    dataList,
    pagination,
    onSearch,
    resetForm,
    send,
    recall,
    addMessage,
    editMessage,
    viewMessage,
    handleDelete,
    handleSizeChange,
    handleCurrentChange
  };
}
